// pages/signUp/signUp.js
var app = getApp()
var util = require("../../utils/util.js");
var config = require("../../utils/config.js");

Page({
  /**
   * 页面的初始数据
   */
  data: {
    screen: app.data.screen,
    hidden: 2,
    message: [],
    imgurl: [],
    returnimg1: '',
    returnimg: [],
    signimg: '',
    inputValue: [],
    count: '',
    isphone: '',
    id: '',
    enroll_id: ''
  },
  portrait(e) {
    var signimg = e.currentTarget.dataset.img;
    var keyimg = e.currentTarget.dataset.key;
    wx.chooseImage({
      sizeType: ['original', 'compressed'],  //可选择原图或压缩后的图片
      sourceType: ['album', 'camera'], //可选择性开放访问相册、相机
      success: res => {
        // 上传预览图片
        const tempFilePaths = res.tempFilePaths
        var imgdata = tempFilePaths[0];
        this.data.imgurl[keyimg] = imgdata;
        var returnimg1;
        var that = this;
        wx.uploadFile({
          url: config.upLoad, // 仅为示例，非真实的接口地址
          filePath: tempFilePaths[0],
          name: 'imgurl',
          // formData: {
          //   imgurl: 'img12'
          // },
          success:function (res1) {
            var jsonString = res1.data;
            if (typeof (jsonString) == 'string') {
              jsonString = jsonString.substring(jsonString.indexOf("{"), jsonString.lastIndexOf("}") + 1);
              res1.data = JSON.parse(jsonString);
            }
            that.data.returnimg[keyimg] = res1.data.data.imgurl; 
            var newimg = [signimg + '|' + that.data.returnimg[keyimg]];//返回的图片路径
            that.data.inputValue[keyimg] = newimg;
            that.setData({
              imgurl: that.data.imgurl,
              isload1: 'none',
              signimg: e.currentTarget.dataset.img,
              inputValue: that.data.inputValue
            });
          }
        })  
               
        // if (res.tempFilePaths.length > 2) {
        //   wx.showToast({
        //     title: '最多只能上传一张图片！',
        //     icon: 'none',
        //     duration: 2000
        //   })
        //   imgurl1[keyimg] = res.tempFilePaths.slice(0, 1);
        // }
   
      }
    })
  },
  // 点击预览
  previewImg (e) {
    var index = e.currentTarget.dataset.index;
    var imgArr = [];
    imgArr.push(this.data.imgurl[index]);
   wx.previewImage({
      current: this.data.imgurl[index],     //当前图片地址
      urls: imgArr,               //所有要预览的图片的地址集合 数组形式
      success (res)  { 

      },
      fail (res) { },
      complete (res) { },
    })
  },
  inputChange(e) {
    console.log(e)
    var that = this;
    var value = e.detail.value
    var key = e.currentTarget.dataset.key
    var sign = e.currentTarget.dataset.sign
    var updateData = that.data.inputValue;
    updateData[key] = [sign + '|' + value];
    that.setData({
      inputValue: updateData
    });  
    console.log(that.data.inputValue)
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log(options)
    this.setData({
      id: options.id,
      enroll_id: options.enroll_id
    })
    var that = this;
    // 查看是否授权
    wx.getSetting({
      success(res) {
        if (res.authSetting['scope.userInfo']) {
        } else {
          wx.navigateTo({
            url: '/pages/login/login',
          });
        }
      }
    })
    if (!that.data.enroll_id) {
      that.request(that.data.id,'0');
    } else {
      that.request(that.data.enroll_id, '1');//重新提交
    }
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  // 防止重复点击
  waiton() {
    this.setData({
      isDisabled: true
    })
    setTimeout(() => {
      this.setData({
        isDisabled: false
      })
    }, 2000);
  },

  //  网络请求
  submitTo(e) {
    var formid = e.detail.formId;
    // if (!this.data.isphone) {
    //   wx.showToast({
    //     title: '输入的手机号码有误，请重新输入',
    //     icon: 'none',
    //     duration: 2000
    //   })
    //   return;
    // }
    if (this.data.inputValue.length < this.data.count) {
      wx.showToast({
        title: '请完善输入信息',
        icon: 'none',
        duration: 2000
      })
      return;
    }
    this.waiton();
    util.getOpenID((e) => {    
      if (!this.data.enroll_id) {
        var parameters = {
          openid: e.openid,
          message: this.data.inputValue,
          id: this.data.id
        }
      } else {
        var parameters = {
          openid: e.openid,
          message: this.data.inputValue,
          enroll_id: this.data.enroll_id
        }
      }
      var parameters1 = {
        openid: e.openid,
        formid: formid,
      }
      // 收集模板信息
      util.collectRequest(config.collectUser, parameters1, (res) => {
        // console.log(res)
      });
      // 修改信息
      util.postRequest(config.sign, parameters, (res) => {
        console.log(res)
        if (res.code == 200) {
          util.showModal2(res.msg, function (res) {
            wx.navigateTo({
              url: '/pages/userCenter/signOrder/signOrder?baoming=0'
            })
          })
        } else if (res.code == 201) {
          util.showModal2(res.msg, '')
        }
      }, () => {
        //请求失败
      }, () => {
        //请求完成
      }, true);
    }, true);
  },
  //  网络请求
  request(id,isid) {
    util.getOpenID((e) => {
      if (isid == '1'){
        var parameters = {
          openid: e.openid,
          enroll_id: id
        }
      } else if(isid == '0') {
        var parameters = {
          openid: e.openid,
          id: id
        }
      }
      util.postRequest(config.signHome, parameters, (res) => {
        console.log(res)
        if (res.code == 200) {
          this.setData({
            hidden: 1,
            message: res.data.table,
            count: res.data.count
          })
        } else if (res.code == 201) {
          wx.showToast({ title: '请求失败!', icon: 'none' })
        }
      }, () => {
        //请求失败
      }, () => {
        //请求完成
      }, true);
    }, true);
  }
})